package wat.serwerapp.usermgmt.ldap;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;

/**
 *
 * @author P.B. & L.G. & K.K.
 */
public class RoleManager {

    private static final String ROLE_SUFIX = ",ou=roles,dc=wat,dc=edu,dc=pl";
    private static final String ROLES = "ou=roles,dc=wat,dc=edu,dc=pl";

    private static Map<String, Map<String, String>> buffer = new HashMap<String, Map<String, String>>();

    public static Map<String, String> getRights(String roleName) throws NamingException {
        Map<String, String> rights = buffer.get(roleName);

        if (rights == null) {
            Role r = new Role(roleName);
            rights = r.getRights();
            buffer.put(roleName, rights);
        }

//        Set<String> setKey = rights.keySet();
//        
//        for (String string : setKey) {
//            System.out.println(string + " - " + rights.get(string));
//        }

        return rights;
    }

    public static void setRights(String roleName, Map<String, String> rights) throws NamingException, Exception {
        Role r = new Role(roleName);
        r.setRights(rights);
        buffer.put(roleName, r.getRights());
    }

    public static void addRight(String roleName, String rightName, String rightFile) throws NamingException, Exception {
        Role r = new Role(roleName);
        r.addRight(rightName, rightFile);
        buffer.put(roleName, r.getRights());
    }

    public static void removeRight(String roleName, String rightName) throws NamingException, Exception {
        Role r = new Role(roleName);
        r.removeRight(rightName);
        buffer.put(roleName, r.getRights());
    }

    public static String roleFromLdapFormat(String role) {
        int beginIndex = 3;
        int endIndex = role.indexOf(ROLE_SUFIX);
        return role.substring(beginIndex, endIndex);
    }

    public static String roleToLdapFormat(String role) {
        return "cn=" + role + ROLE_SUFIX;
    }

    public static Set<String> getAllRolesNames() {
        try {
            System.out.println("getAllRolesNames method...");
            HashSet<String> roles = new HashSet<String>();
            DirContext ctx = LdapConnector.getRolesContext();
            NamingEnumeration<NameClassPair> list = ctx.list(ROLES);
            while (list.hasMore()) {
                NameClassPair nc = list.next();

                String cn = nc.getName();
                roles.add(cn);
            }
            return roles;
        } catch (NamingException ex) {
            Logger.getLogger(RoleManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static void addRole(String roleName, Map<String, String> rights) {
        try {
            System.out.println("addRole method...");
            DirContext ctx = LdapConnector.getRolesContext();

            Attributes att = new BasicAttributes();
            Attribute c = new BasicAttribute("objectClass");
            c.add("groupOfUniqueNames");
            c.add("top");
            att.put(c);

            if (rights.size() > 0) {
                Attribute b = new BasicAttribute(Role.FILE_ATTRIBUTE_NAME);
                Attribute a = new BasicAttribute(Role.PARAMETER_ATTRIBUTE_NAME);
                att.put(a);
                att.put(b);

                Set<String> rightsKeys = rights.keySet();
                for (String right : rightsKeys) {
                    a.add(right);
                    b.add(rights.get(right));
                }
            }
            ctx.createSubcontext(RoleManager.roleToLdapFormat(roleName), att);
        } catch (NamingException ex) {
            Logger.getLogger(RoleManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void removeRole(String roleName) {
        try {
            DirContext ctx = LdapConnector.getRolesContext();
            ctx.destroySubcontext(roleToLdapFormat(roleName));
        } catch (NamingException ex) {
            Logger.getLogger(RoleManager.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
